<template>
    <el-dialog
            :title="!dataForm.id ? '新增' : '修改'"
            :close-on-click-modal="false"
            :visible.sync="visible">
        <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="120px">
                            <el-form-item label="权限ID，主键" prop="id">
                        <el-input v-model.number="dataForm.id" placeholder="权限ID，主键"></el-input>
                </el-form-item>
                <el-form-item label="文档ID（外键关联文档表）" prop="documentId">
                        <el-input v-model.number="dataForm.documentId" placeholder="文档ID（外键关联文档表）"></el-input>
                </el-form-item>
                <el-form-item label="用户ID（外键关联用户表，可为空）" prop="userId">
                        <el-input v-model.number="dataForm.userId" placeholder="用户ID（外键关联用户表，可为空）"></el-input>
                </el-form-item>
                <el-form-item label="团队ID（外键关联团队表，可为空）" prop="teamId">
                        <el-input v-model.number="dataForm.teamId" placeholder="团队ID（外键关联团队表，可为空）"></el-input>
                </el-form-item>
                <el-form-item label="权限类型" prop="permission">
                        <el-input v-model="dataForm.permission" placeholder="权限类型"></el-input>
                </el-form-item>
                <el-form-item label="继承自的权限ID（外键关联本表，可为空）" prop="inheritFrom">
                        <el-input v-model.number="dataForm.inheritFrom" placeholder="继承自的权限ID（外键关联本表，可为空）"></el-input>
                </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
    </el-dialog>
</template>

<script>
    export default {
      data () {
        return {
          visible: false,
          dataForm: {
            id: null,
            documentId: null,
            userId: null,
            teamId: null,
            permission: null,
            inheritFrom: null
          },
          dataRule: {
            id: [
                        { required: true, message: '权限ID，主键不能为空', trigger: 'blur' }
            ],
            documentId: [
                        { required: true, message: '文档ID（外键关联文档表）不能为空', trigger: 'blur' }
            ],
            userId: [
                        { required: true, message: '用户ID（外键关联用户表，可为空）不能为空', trigger: 'blur' }
            ],
            teamId: [
                        { required: true, message: '团队ID（外键关联团队表，可为空）不能为空', trigger: 'blur' }
            ],
            permission: [
                        { required: true, message: '权限类型不能为空', trigger: 'blur' }
            ],
            inheritFrom: [
                        { required: true, message: '继承自的权限ID（外键关联本表，可为空）不能为空', trigger: 'blur' }
            ]
          }
        }
      },
      methods: {
        init (id) {
          this.dataForm.id = id || 0
          this.visible = true
          this.$nextTick(() => {
            this.$refs['dataForm'].resetFields()
            this.dataForm.id = id || 0
            if (this.dataForm.id) {
              this.$http({
                url: this.$http.adornUrl(`/app/document_permission/info/${this.dataForm.id}`),
                method: 'get',
                params: this.$http.adornParams()
              }).then(({data}) => {
                if (data && data.code === 0) {
                  this.dataForm = data.appdocumentpermission
                }
              })
            }
          })
        },
            // 表单提交
        dataFormSubmit () {
          this.$refs['dataForm'].validate((valid) => {
            if (valid) {
              this.$http({
                url: this.$http.adornUrl(`/app/document_permission/${!this.dataForm.id ? 'save' : 'update'}`),
                method: 'post',
                data: this.$http.adornData(this.dataForm)
              }).then(({data}) => {
                if (data && data.code === 0) {
                  this.$message({
                    message: '操作成功',
                    type: 'success',
                    duration: 1500,
                    onClose: () => {
                      this.visible = false
                      this.$emit('refreshDataList')
                    }
                  })
                } else {
                  this.$message.error(data.msg)
                }
              })
            }
          })
        }
      }
    }
</script>